<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\CourseLecturer;
use App\Services\CourseLecturerService;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class CourseLecturerController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'name' => 'required|string',
            'pic' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        CourseLecturer::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'name' => 'required|string',
            'pic' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $course = new CourseLecturer();
        $course->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'name' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = CourseLecturerService::getInstance()->list($page,$limit,$sort,$order, $name);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $slide = CourseLecturer::find($request->id);
        $slide->delete();
        return $this->success();
    }
}
